package com.guli.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.guli.common.utils.Query;
import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.service.AttrService;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @SuppressWarnings("ALL")
    @Autowired
    AttrGroupDao attrGroupDao;

    @SuppressWarnings("ALL")
    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(String type,Long catelogId,Map<String, Object> params) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");

        if (type.equals("base")){
            wrapper.eq("attr_type",1);
        }else {
            wrapper.eq("attr_type",0);
        }
        if (!catelogId.equals(0l)){
            wrapper.eq("catelog_id",catelogId);
        }
        if (StringUtils.isNotEmpty(key)){
            wrapper.like("attr_name",key);
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper

        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> list2(Long attrGroupId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_type",1);
        wrapper.eq("attr_group_id",attrGroupId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public PageUtils list3(Long attrGroupId, Map<String, Object> params) {

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("attr_type",1);
        wrapper.isNull("attr_group_id");

        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper

        );
        return new PageUtils(page);
    }

    @Override
    public void update2(List<AttrEntity> attrs) {
        List<AttrEntity> collect = attrs.stream().map(item -> {
            Long groupid = item.getAttrGroupId();
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(groupid);
            item.setAttrGroupName(attrGroupEntity.getAttrGroupName());
            return item;
        }).collect(Collectors.toList());

        this.updateBatchById(collect);
    }

    @Override
    public void yichu(List<AttrEntity> data) {
        List<AttrEntity> collect = data.stream().map(item -> {
            //规格id
            Long attrId = item.getAttrId();
            //分组id
            Long attrGroupId = item.getAttrGroupId();
            AttrEntity attrEntity = baseMapper.selectById(attrId);
            attrEntity.setAttrGroupName(null);
            attrEntity.setAttrGroupId(null);
            return attrEntity;
        }).collect(Collectors.toList());

        this.updateBatchById(collect);
    }

    @Override
    public boolean save(AttrEntity entity) {

        Long cid = entity.getCatelogId();
        Long aid = entity.getAttrGroupId();
        if (cid!=null){
            CategoryEntity categoryEntity = categoryDao.selectById(cid);
            entity.setCatelogName(categoryEntity.getName());
        }
        if (aid!=null){
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(aid);
            entity.setAttrGroupName(attrGroupEntity.getAttrGroupName());
        }

        return super.save(entity);
    }


    @Override
    public boolean updateById(AttrEntity entity) {
        Long cid = entity.getCatelogId();
        Long aid = entity.getAttrGroupId();
        if (cid!=null){
            CategoryEntity categoryEntity = categoryDao.selectById(cid);
            entity.setCatelogName(categoryEntity.getName());
        }
        if (aid!=null){
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(aid);
            entity.setAttrGroupName(attrGroupEntity.getAttrGroupName());
        }else {
            entity.setAttrGroupId(null);
            entity.setAttrGroupName("");
        }

        return super.updateById(entity);
    }

    @Override
    public AttrEntity getById(Serializable id) {
        AttrEntity attrEntity = baseMapper.selectById(id);
        Long catelogId = attrEntity.getCatelogId();
//        Long attrGroupId = attrEntity.getAttrGroupId();
//        if (attrGroupId==null){
//            attrEntity.setAttrGroupId(null);
//        }
        ArrayList<Long> list = new ArrayList<>();
        //调用私有方法拿到二级与一级的id并且存放到集合里面
        getcatelparnet(catelogId,list);

        //反转
        Collections.reverse(list);
        attrEntity.setArrss(list.toArray(new Long[list.size()]));
        //转成数组把arrlist存进去
        return attrEntity;
    }
    private void getcatelparnet(Long catelogId,ArrayList<Long> list){
        //先把三级id存进去
        list.add(catelogId);
        //通过类型三级id 查询三级类型对象
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if (!categoryEntity.getParentCid().equals(0l)){
            Long parentCid = categoryEntity.getParentCid();
            getcatelparnet(parentCid,list);
        }
    }
}